package at.fhj.itm10.ticketmaster.services.impl;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.log4j.Logger;
import at.fhj.itm10.ticketmaster.services.CartService;

public class CartServiceImpl implements CartService{

	private final Logger logger = Logger.getLogger(CartServiceImpl.class);
	private int totalTicketCount;
	private List<Double> priceList = new ArrayList<Double>();
	private boolean ticketamountisFaild; 
	private double totalPrice;
	private String ticketQuantity;
	private HashMap<String, Integer> tickets = new HashMap<String, Integer>();
	private static Map<String, Object> quantities;
	
	static{
		quantities = new LinkedHashMap<String, Object>();
		quantities.put("0", "0");
		quantities.put("1", "1");
		quantities.put("2", "2");
		quantities.put("3", "3");
		quantities.put("4", "4");
		quantities.put("5", "5");
		quantities.put("6", "6");
	}
	
	public Boolean addToCard(String id) {
		logger.debug("addToCart(" + id + ")");
		ticketamountisFaild = false;
		logger.debug("getTicketQuantity(" + getTicketQuantity() + ")");
		int quantity = Integer.parseInt(getTicketQuantity());
		
		String theMessage = null;

		if (tickets.containsKey(id) == true) {
			if (tickets.get(id) + quantity < 7) {
				totalTicketCount = totalTicketCount
						+ quantity;
				int newQuantity = tickets.get(id) + quantity;
				tickets.put(id, newQuantity);
				theMessage = quantity + " Tickets für Event " + id
						+ " hinzugefügt!";
				System.out.println("1die event id = " + id + ". Die id in der hashmap= "+ tickets);

			} else {
				theMessage = "!Mehr als 6 Tickets für Event " + id
						+ " im Warenkorb!";
				ticketamountisFaild = true;
				System.out.println("2die event id = " + id + ". Die id in der hashmap= "+ tickets);

			}
		} else {
				tickets.put(id, quantity);
				totalTicketCount = totalTicketCount
						+ quantity;
				theMessage = quantity + " Tickets für Event " + id
						+ " hinzugefügt!";
				System.out.println("3die event id = " + id + ". Die id in der hashmap= "+ tickets);
		}
		System.out.println(theMessage);
		return ticketamountisFaild;
	}

	public void addTicketPrice(double singlePrice, int quantity) {
		logger.debug("addTicketPrice(" + singlePrice + quantity + ")");
		
		priceList.add(singlePrice * ((double)quantity));

	}

	public Boolean getticketamountisFaild() {
		logger.debug("getticketamountisFaild()" );
		
		return ticketamountisFaild;
	}

	public String getTotalPrice() {
		logger.debug("getTotalPrice()");
		
		DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.ENGLISH);
		otherSymbols.setDecimalSeparator('.');

		DecimalFormat df=new DecimalFormat("0.00", otherSymbols);
 
		totalPrice=0.0;
		
		for (double price : priceList) {
			totalPrice += price;
		}
		
		String finalPrice = df.format(totalPrice); 
		
		priceList.clear();
		return finalPrice;	
	}

	public String removeFromCart(String id) {
		logger.debug("removeFromCart(" + id + ")");
		String theMessage = null;
			
		if (tickets.containsKey(id) == true) {
			totalTicketCount = totalTicketCount - tickets.get(id);
			tickets.remove(id);
						
		} else {
			theMessage = "Keine Tickets für das Event " + id + " im Warenkorb!";
		}
	
		return theMessage;
	}

	public void emptyCart() {
		logger.debug("emptyCart()");
		totalTicketCount = 0;
		tickets.clear();
	}

	public void saveOrder(Long cust_id) {
		logger.debug("saveOrder(" + cust_id + ")");
		// Aufruf hidden field orderconfirmation.xhtml		
		emptyCart();
	}

	public int getTotalTicketCount() {
		logger.debug("getTotalTicketCount()");
		return totalTicketCount;
	}

	public void setTotalTicketCount(int totalTicketCount) {
		logger.debug("setTotalTicketCount(" + totalTicketCount + ")");

		this.totalTicketCount = totalTicketCount;
	}

	/*public void setTotalPrice(double totalPrice) {
		logger.debug("setTotalPrice(" + totalPrice + ")");
		this.totalPrice = totalPrice;
	}*/

	public String getTicketQuantity() {
		logger.debug("getTicketQuantity()");
		return ticketQuantity;
	}

	public void setTicketQuantity(String ticketQuantity) {
		logger.debug("setTicketQuantity(" + ticketQuantity + ")");
		this.ticketQuantity = ticketQuantity;
	}

	public List<Entry<String, Integer>> getTicketsList() {
		logger.debug("getTicketsList()");
		return new ArrayList(tickets.entrySet());
	}

	public HashMap<String, Integer> showTickets() {
		logger.debug("showTickets()");
		return tickets;
	}

	public Map<String, Object> getQuantities() {
		logger.debug("getQuantities()");
		return quantities;
	}

}
